package online.westonline.com.westonline.ui.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.easy.west.westclient.Constants;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.base.BaseFragment;
import online.westonline.com.westonline.utils.ToastUtils;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 质询
 * Created by xiatao on 2018/7/31.
 */

public class NewsFragment extends BaseFragment {
    @BindView(R.id.web_news_view)
    WebView webNewsView;
    Unbinder unbinder;
    @BindView(R.id.pro_my_webview)
    ProgressBar proMyWebview;
    Unbinder unbinder1;
    @BindView(R.id.btn_back)
    FloatingActionButton btnBack;
    Unbinder unbinder2;
    Unbinder unbinder3;

    @Override
    protected boolean setEventOpen() {
        return false;
    }

    @Override
    protected void initdata() {

    }

    @Override
    protected void initview() {
        webNewsView.loadUrl(Constants.WEB_VIEW);
        btnBack.setBackgroundTintList(ColorStateList.valueOf(UiUtlis.getColor(R.color.Translate)));
        webNewsView.getSettings().setJavaScriptEnabled(true);
        webNewsView.getSettings().setAppCacheEnabled(false);
        //设置 缓存模式
        webNewsView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webNewsView.getSettings().setDomStorageEnabled(true);
        webNewsView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webNewsView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress <= 100) {
                    proMyWebview.setVisibility(View.VISIBLE);
                    proMyWebview.setProgress(newProgress);
                }
                if (newProgress == 100) {
                    proMyWebview.setVisibility(View.GONE);
                }

            }
        });
        //回退
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webNewsView.canGoBack())
                {
                    webNewsView.goBack();
                }
                else
                {
                    ToastUtils.showToast("您已经回到首页");
                }
            }
        });


    }

    @Override
    protected int setlayout() {
        return R.layout.fragment_news;
    }


}
